﻿package wibrst.visual {
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import wibrst.utils.VisualUtil;
	
	/**
	 * ...
	 * @author wibrst@qq.com
	 */
	public class VImage extends Sprite {
		
		static public const IMG_LOADED:String = "IMG_LOADED";
		
		public function get disp():Sprite { return _disp; }
		public function set disp(value:Sprite):void {
			_disp = value;
		}
		
		public function get bLoaded():Boolean { return _bLoaded; }
		
		public function get visual():DisplayObject { return _visual; }
		
		public function VImage( w:Number = 0, h:Number = 0, uImg:String = null ) {
			_bLoaded = false;
			if (uImg)
				loadImg(uImg);
				
			if( w != 0 && h != 0 ){
				_shpImageMask = new Shape;
				_shpImageMask.graphics.beginFill( 0, .2 );
				_shpImageMask.graphics.drawRect( 0, 0, w, h );
				_shpImageMask.graphics.endFill();
				addChild( _shpImageMask );
				
				_rectHolder = new Rectangle( 0, 0, w, h );
			}
			
			_disp = new Sprite;
			addChild(_disp);
			_disp.mask = _shpImageMask;
		}
		
		public function reset():void {
			if ( _disp.mask ) _disp.mask = null;
			unloadImg();
			
			_disp.scaleX = _disp.scaleY = 1;
			_disp.rotation = 0;
		}
		
		public function showMask():void {
			disp.mask = null;
			_maskCustom.visible = true;
		}
		
		public function setCustomMask( maskCustom:Sprite, bStartNoMask:Boolean = false ):void {
			
			if ( _disp.mask ) {
				if ( _disp.mask.parent){
					_disp.mask.parent.removeChild( _disp.mask );
				}
				_disp.mask = null;
			}
			
			_maskCustom = maskCustom;
			_maskCustom.visible = false;
			addChild( _maskCustom );
			//if( _shpImageMask) VisualUtil.setDisplayCenterPointOfParent( _maskCustom, _shpImageMask );
			
			if( !bStartNoMask ) _disp.mask = _maskCustom;
		}
		
		/// ---------- display img way 1: direct display gived bmp
		public function dispImg( tgt:DisplayObject ,bFitOnRect:Boolean=true ,bFitFill:Boolean=true ,bSetCenter:Boolean=true ):void {
			unloadImg();
			
			if (tgt != null) {
				
				tgt.x = 0;
				tgt.y = 0;
				if ( tgt.z ) tgt.z = 0;
				
				tgt.scaleX = 1;
				tgt.scaleY = 1;
				//if ( tgt.scaleZ ) tgt.scaleZ = 0;
				
				_visual = tgt;
				_disp.addChild( tgt );
				//trace*( this, "dispImg:", tgt.width, "*", tgt.height, tgt.stage, tgt.root );
			
				if(bSetCenter) VisualUtil.setDisplayCenterPointOfParent( tgt, _disp );
				if(bFitOnRect && _rectHolder ) VisualUtil.fitDispOnRect( _disp, _rectHolder, bFitFill );
			
				if(_maskCustom) _disp.mask = _maskCustom;	// if not defined ,it's null, just disable mask
				_disp.visible = true;
			}
		}
		
		/// ---------- display img way 2: from a url load the image
		public function loadImg( uImg:String ,bFitOnRect:Boolean=true ):void {
			//trace*( this, "loadImg:", uImg );
			_loader = new Loader;
			_loader.load( new URLRequest(uImg) );
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompHandler);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			
			_bFitOnRectLoadSession = bFitOnRect;
		}
		
		public function unloadImg():void {
			for (; _disp.numChildren; ) _disp.removeChildAt(0);
		}
		
		private function loadCompHandler(e:Event):void {
			//trace*( this, "loadCompHandler:", e, this.visible );
			var tgt:DisplayObject = _loader.content;
			dispImg( tgt, _bFitOnRectLoadSession );
			
			_bLoaded = true;
			
			dispatchEvent( new Event( IMG_LOADED ) );
		}
		
		private function ioErrorHandler(e:IOErrorEvent):void {
			trace( this, e );
		}
		
		private var _shpImageMask:Shape;
		private var _disp:Sprite;
		
		private var _loader:Loader;
		private var _rectHolder:Rectangle;
		private var _maskCustom:Sprite;
		private var _bFitOnRectLoadSession:Boolean;
		private var _bLoaded:Boolean;
		private var _visual:DisplayObject;
		
	}

}